package com.example.widgethost.base;

import android.appwidget.AppWidgetHost;
import android.appwidget.AppWidgetHostView;
import android.content.Context;
import android.view.ViewGroup;

import com.example.widgethost.base.cache.CacheWidgetBean;

import java.util.List;

public abstract class CacheContainerConstructor<T extends ViewGroup, S extends ViewGroup.LayoutParams, K extends AppWidgetHost> extends BaseContainerConstructor<T, S, K> {
    private CacheTool cacheTool;

    /**
     * constructor.
     *
     * @param context context
     * @param t       container layout
     */
    public CacheContainerConstructor(Context context, T t) {
        super(context, t);
        cacheTool = new CacheTool(context);
        setCacheInterface(cacheTool);
        addCachedWidgets();
    }

    /**
     * save added widgets info.
     */
    private void addCachedWidgets() {
        List<CacheWidgetBean> cacheWidgetBeans = cacheTool.getCachedWidgetBeans();
        if (cacheWidgetBeans != null && cacheWidgetBeans.size() > 0) {
            for (CacheWidgetBean cacheWidgetBean : cacheWidgetBeans) {
                List<Integer> ids = cacheWidgetBean.widgetIds;
                for (int id : ids) {
                    AppWidgetHostView appWidgetHostView = createWidgetFromId(id);
                    addWdigetToHost(appWidgetHostView);
                }
            }
        }
    }
}
